package net.yxsoft.service.formdisigner;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import net.sf.ehcache.search.parser.ParseException;
import net.yxsoft.enumclass.FormDataRoleType;
import net.yxsoft.kit.ToolDateTime;
import net.yxsoft.model.formdesigner.FnFormDataRole;
import net.yxsoft.service.common.BaseService;
import rapid.core.Authuser;
import rapid.core.TResult;
import rapid.util.StrKit;

import java.util.List;

/**
 * @author Administrator
 */
public class FormDataRoleService extends BaseService {


  /**
  * @MethodName: add
  * @Description: 保存
  * @param: obj
   * @param: user
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 13:45
  * @author wj 
  */
  public TResult add(FnFormDataRole obj, Authuser user) throws ParseException {
    if (obj == null) {
      return fail();
    }

    String _now = ToolDateTime.format(ToolDateTime.getDate(), "yyyy-MM-dd hh:mm:ss");

    if (FormDataRoleType.Audit.getValue().equals(obj.getAttrib01())) {

      List<FnFormDataRole> roles = FnFormDataRole.dao.find("select * from fn_form_data_role where row_id = ? and attrib_01 = ?", obj.getRowId(), obj.getAttrib01());

      if (roles != null && roles.size() > 4) {

        return TResult.fail("已经设置了5级审核角色，不可再设置。");
      }

      if (null == obj.getAttrib11()) {

        return TResult.fail("审核角色必须设置审核顺序");
      }

    }

    List<FnFormDataRole> roles = FnFormDataRole.dao.find("select * from fn_form_data_role where row_id = ? and attrib_02 = ?", obj.getRowId(), obj.getAttrib02());

    if (roles != null && roles.size() > 0) {
      return TResult.fail("已经设置了此角色的操作权限，不可再设置。");
    }


    obj.setCreated(ToolDateTime.getDate());
    obj.setCreatedBy(user.getId());
    obj.setCreatedByName(user.getName());
    obj.setLastUpd(ToolDateTime.getDate());
    obj.setLastUpdBy(user.getId());
    obj.setLastUpdByName(user.getName());


    return obj.save() ? success() : fail();
  }
  
  /**
  * @MethodName: update
  * @Description: 保存
  * @param: obj
   * @param: user
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 13:45
  * @author wj 
  */
  public TResult update(FnFormDataRole obj, Authuser user) throws ParseException {
    if (obj == null) {
      return fail();
    }
    String now = ToolDateTime.format(ToolDateTime.getDate(), "yyyy-MM-dd hh:mm:ss");

    if (FormDataRoleType.Audit.getValue().equals(obj.getAttrib01())) {
      //审核角色只能是一个

      FnFormDataRole role = FnFormDataRole.dao.findFirst("select * from fn_form_data_role where row_id=? and attrib_01 = ? and conflict_id <> ?", obj.getRowId(), obj.getAttrib01(), obj.getConflictId());

      if (null == obj.getAttrib11()) {
        return fail("审核角色必须设置审核顺序");
      }

    } else {
      FnFormDataRole _role = FnFormDataRole.dao.findFirst("select * from fn_form_data_role where row_id=?  and attrib_02=? and conflict_id <> ? ", obj.getRowId(), obj.getAttrib02(), obj.getConflictId());

      if (_role != null) {
        return fail("已经设置了此角色的操作权限，不可再设置。");
      }
    }

    FnFormDataRole _old = FnFormDataRole.dao.findById(obj.getConflictId());

    _old.setLastUpd(ToolDateTime.getDate());
    _old.setLastUpdBy(user.getId());
    _old.setLastUpdByName(user.getName());
    _old.setAttrib01(obj.getAttrib01());
    _old.setAttrib02(obj.getAttrib02());
    _old.setAttrib03(obj.getAttrib03());
    _old.setAttrib11(obj.getAttrib11());
    _old.setAttrib15(obj.getAttrib15());

    return obj.update() ? success() : fail();
  }
  
  /**
  * @MethodName: remove
  * @Description:  删除
  * @param: ids
   * @param: user
  * @return * @return rapid.core.TResult
  * @throws 
  * @date 2023/8/31 13:46
  * @author wj 
  */
  public TResult remove(String ids, Authuser user) throws ParseException {
    if (StrKit.isBlank(ids)) {
      return fail();
    }

    String[] _ids = ids.split(",");
    try {
      for (int i = 0; i < _ids.length; i++) {

        FnFormDataRole _old = FnFormDataRole.dao.findById(_ids[i]);

        if (_old == null) {
          return fail().msg("记录不存在或已被删除。");
        }

        FnFormDataRole.dao.deleteById(_ids[i]);
      }
    } catch (Exception ex) {
      return fail();
    }

    return success();
  }

  /**
  * @MethodName: getFormDataUserRole
  * @Description: 
  * @param: formId
   * @param: user
  * @return * @return com.jfinal.plugin.activerecord.Record
  * @throws 
  * @date 2023/8/31 13:46
  * @author wj 
  */
  public Record getFormDataUserRole(String formId, Authuser user) {

    Record result = Db.findFirst("select a.attrib_01,a.attrib_03,b.name rolename,b.code rolecode,a.attrib_15 from fn_form_data_role a\n" +
        "  left join ts_role b on a.attrib_02=b.id\n" +
        "  left join ts_ur_relation c on c.role_id = b.id\n" +
        "  left join ts_user d on c.user_id = d.id\n" +
        "where a.row_id = ? and d.id = ?", formId, user.getId());
    return result;
  }

  /**
  * @MethodName: getFormDataUseraudit
  * @Description: 
  * @param: formId
 * @param: role
  * @return * @return int
  * @throws 
  * @date 2023/8/31 14:22
  * @author wj 
  */
  public int getFormDataUseraudit(String formId,String role) {

    return Db.find("select a.attrib_01 from fn_form_data_role a\n" +
            "where a.row_id = ? and a.attrib_01=? ", formId,role).size();
  }



}
